function f = objectiveFcn(x)
DSM=zeros(17,17);
DSM(1,3)=1; DSM(2,3)=1; DSM(3,4)=1; DSM(3,5)=1; DSM(3,6)=1;
DSM(4,3)=1; DSM(4,9)=1; DSM(5,3)=1; DSM(5,14)=1; DSM(6,3)=1;
DSM(6,12)=1; DSM(7,6)=1; DSM(8,4)=1; DSM(9,13)=1; DSM(10,5)=1;
DSM(11,5)=1; DSM(12,16)=1; DSM(13,8)=1; DSM(14,15)=1;
DSM(15,10)=1; DSM(15,11)=1; DSM(16,7)=1; DSM(17,5)=1;

a = DSM;
m = size(a,2);
n = 9;
fi = 0.5;

num = zeros(1,n);
for i=1:n
    temp = (x((i-1)*m+1:m*i) ~= 0);
    num(i) = sum(temp(:));
end

DSM = cell(1,n);
index = cell(1,n);
for i=1:n
    if num(i) > 0
        DSM{i} = zeros(num(i), num(i));
        index{i} = find(x((i-1)*m+1:m*i) ~= 0);
        for ii=1:num(i)
            for jj=1:num(i)
                DSM{i}(ii,jj) = a(index{i}(ii), index{i}(jj));
            end
        end
    else
        DSM{i} = zeros(0,0);
        index{i} = [];
    end
end

% Complexity calculation
alpha = cell(1,n+1);
for i=1:n
    alpha{i} = ones(1, num(i));
end
alpha{n+1} = zeros(1,n);

for i=1:n
    if num(i) > 0
        C1_temp = individual_Complexity(alpha{i});
        C2_temp = interaction_Complexity(DSM{i}, fi);
        C3_temp = topological_Complexity(DSM{i});
        C_total_temp = [C1_temp C2_temp C3_temp];
        alpha{n+1}(i) = total_Complexity(C_total_temp);
    else
        alpha{n+1}(i) = 0;  % 空模块复杂度设为0
    end
end

% DSM_new
DSM_new = zeros(n,n);
for i=1:n
    for j=1:n
        if i ~= j && ~isempty(index{i}) && ~isempty(index{j})
            for k1 = 1:length(index{i})
                for k2 = 1:length(index{j})
                    if a(index{i}(k1), index{j}(k2)) ~= 0
                        DSM_new(i,j) = DSM_new(i,j) + a(index{i}(k1), index{j}(k2));
                    end
                end
            end
        end
    end
end

% Final complexity
try
    C1_new = individual_Complexity(alpha{n+1});
    C2_new = interaction_Complexity(DSM_new, fi);
    C3_new = topological_Complexity(DSM_new);
    C_total_new = [C1_new C2_new C3_new];
    C_new = total_Complexity(C_total_new);

    if isnan(C_new)
        f = 1e6;  % NaN 惩罚
    else
        f = C_new;
    end
catch
    f = 1e6;  % 错误惩罚
end
end
